<?php

namespace App\Http\Controllers\Wap;

use App\Http\Util\Result;
use App\Model\Member;
use App\Model\OrderList;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Payment\Order;
use Illuminate\Support\Facades\DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;

class BaseController extends Controller
{
    public function home() {
        $app = app('wechat');
        $oauth = $app->oauth;
        // 判断是否刚登录过
        if (empty(session('wechat_user'))) {
            session(['target_url' =>'/wap/member']);
            return $oauth->redirect();
        } else {
            $openid = session('wechat_user')['id'];
            $model = DB::table('members')->where('openid',$openid);
            //判断是否有openid
            if (empty($model->count())) {
                $unionid = session('wechat_user')['original']['unionid'];
                $sel = DB::table('members')->where('unionid',$unionid);
                //判断是否有unionid
                if (empty($sel->count())) {
                    $targetUrl = '/wap/member/bindTel';
                } else {
                    $sel->update(['openid'=>$openid]);
                    $member = $model->first();
                    session(['member'=>$member]);
                    $targetUrl = '/wap/member';
                }
            } else {
                $member = $model->first();
                session(['member'=>$member]);
                $targetUrl = '/wap/member';
            }
            return redirect($targetUrl);
        }
    }

    public function mid() {
        $app = app('wechat');
        $oauth = $app->oauth;
        // 判断是否刚登录过
        if (empty(session('wechat_user'))) {
            session(['target_url' =>'/wap/member/mid']);
            return $oauth->redirect();
        } else {
            $openid = session('wechat_user')['id'];
            $model = DB::table('members')->where('openid',$openid);
            //判断是否有openid
            if (empty($model->count())) {
                $unionid = session('wechat_user')['original']['unionid'];
                $sel = DB::table('members')->where('unionid',$unionid);
                //判断是否有unionid
                if (empty($sel->count())) {
                    $targetUrl = '/wap/member/bindTel';
                } else {
                    $sel->update(['openid'=>$openid]);
                    $member = $model->first();
                    session(['member'=>$member]);
                    $targetUrl = '/wap/member/mid';
                }
            } else {
                $member = $model->first();
                session(['member'=>$member]);
                $targetUrl = '/wap/member/mid';
            }
            return redirect($targetUrl);
        }
    }

    public function goods() {
        $app = app('wechat');
        $oauth = $app->oauth;
        // 判断是否刚登录过
        if (empty(session('wechat_user'))) {
            session(['target_url' =>'/wap/goods/list?id=0']);
            return $oauth->redirect();
        } else {
            $openid = session('wechat_user')['id'];
            $model = DB::table('members')->where('openid',$openid);
            //判断是否有openid
            if (empty($model->count())) {
                $unionid = session('wechat_user')['original']['unionid'];
                $sel = DB::table('members')->where('unionid',$unionid);
                //判断是否有unionid
                if (empty($sel->count())) {
                    $targetUrl = '/wap/member/bindTel';
                } else {
                    $sel->update(['openid'=>$openid]);
                    $member = $model->first();
                    session(['member'=>$member]);
                    $targetUrl = '/wap/goods/list?id=0';
                }
            } else {
                $member = $model->first();
                session(['member'=>$member]);
                $targetUrl = '/wap/goods/list?id=0';
            }
            return redirect($targetUrl);
        }
    }

    public function homeActive() {
        session(['bindUrl' =>1]);
        $app = app('wechat');
        $oauth = $app->oauth;
        // 判断是否刚登录过
        if (empty(session('wechat_user'))) {
            session(['target_url' =>'/wap/cardActive']);
            return $oauth->redirect();
        } else {
            $openid = session('wechat_user')['id'];
            $model = DB::table('members')->where('openid',$openid);
            //判断是否有openid
            if (empty($model->count())) {
                $unionid = session('wechat_user')['original']['unionid'];
                $sel = DB::table('members')->where('unionid',$unionid);
                //判断是否有unionid
                if (empty($sel->count())) {
                    $targetUrl = '/wap/member/bindTel';
                } else {
                    $sel->update(['openid'=>$openid]);
                    $member = $model->first();
                    session(['member'=>$member]);
                    $targetUrl = '/wap/cardActive';
                }
            } else {
                $member = $model->first();
                session(['member'=>$member]);
                $targetUrl = '/wap/cardActive';
            }
            return redirect($targetUrl);
        }
    }

    public function dzm() {
        session(['bindUrl' =>2]);
        $app = app('wechat');
        $oauth = $app->oauth;
        // 判断是否刚登录过
        if (empty(session('wechat_user'))) {
            session(['target_url' =>'/wap/card/dzm']);
            return $oauth->redirect();
        } else {
            $openid = session('wechat_user')['id'];
            $model = DB::table('members')->where('openid',$openid);
            //判断是否有openid
            if (empty($model->count())) {
                $unionid = session('wechat_user')['original']['unionid'];
                $sel = DB::table('members')->where('unionid',$unionid);
                //判断是否有unionid
                if (empty($sel->count())) {
                    $targetUrl = '/wap/member/bindTel';
                } else {
                    $sel->update(['openid'=>$openid]);
                    $member = $model->first();
                    session(['member'=>$member]);
                    $targetUrl = '/wap/card/dzm';
                }
            } else {
                $member = $model->first();
                session(['member'=>$member]);
                $targetUrl = '/wap/card/dzm';
            }
            return redirect($targetUrl);
        }
    }
}
